package com.cv.platform.service.message;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.platform.controller.platform.message.vo.conversation.CvConversationSaveReqVO;
import com.cv.platform.controller.platform.message.vo.conversation.CvConversationVisibleSaveReqVO;
import com.cv.platform.dal.dataobject.message.CvConversationDO;
import com.cv.platform.dal.mapper.message.CvConversationMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 私信会话业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvConversationServiceImpl implements CvConversationService {

    @Resource
    private CvConversationMapper cvConversationMapper;

    @Resource
    private CvConversationVisibleService cvConversationVisibleService;

    @Override
    public Long saveConversation(CvConversationSaveReqVO reqVO) {
        CvConversationDO cvConversationDO = BeanUtils.toBean(reqVO, CvConversationDO.class);
        cvConversationDO.setCreateUserId(SecurityFrameworkUtil.getLoginUserId());

        cvConversationMapper.insert(cvConversationDO);
        Long conversationId = cvConversationDO.getId();

        CvConversationVisibleSaveReqVO saveReqVO = new CvConversationVisibleSaveReqVO()
                .setConversationId(conversationId)
                .setUserId(SecurityFrameworkUtil.getLoginUserId());
        cvConversationVisibleService.saveConversationVisible(saveReqVO);

        saveReqVO.setUserId(reqVO.getTargetUserId());
        cvConversationVisibleService.saveConversationVisible(saveReqVO);

        return conversationId;
    }

    @Override
    public Long getConversationId(Long userId, Long targetId) {
        LambdaQueryWrapper<CvConversationDO> wrapper = new LambdaQueryWrapperX<CvConversationDO>()
                .eq(CvConversationDO::getCreateUserId, userId)
                .eq(CvConversationDO::getTargetUserId, targetId)
                .or(
                        o -> o
                                .eq(CvConversationDO::getCreateUserId, targetId)
                                .eq(CvConversationDO::getTargetUserId, userId)
                );

        CvConversationDO cvConversationDO = cvConversationMapper.selectOne(wrapper);
        if (ObjUtil.isEmpty(cvConversationDO)) {
            return null;
        }
        Long conversationDOId = cvConversationDO.getId();
        cvConversationVisibleService.setVisible(conversationDOId, true);
        return conversationDOId;
    }

    @Override
    public List<Long> getConversationIdList() {
        Long userId = SecurityFrameworkUtil.getLoginUserId();

        LambdaQueryWrapper<CvConversationDO> wrapper = new LambdaQueryWrapperX<CvConversationDO>()
                .eq(CvConversationDO::getCreateUserId, userId)
                .or(
                        o -> o
                                .eq(CvConversationDO::getTargetUserId, userId)
                );

        List<CvConversationDO> cvConversationDOList = cvConversationMapper.selectList(wrapper);

        // 获取可见的会话
        cvConversationDOList.removeIf(cvConversationDO ->
                !cvConversationVisibleService.isVisible(cvConversationDO.getId())
        );

        if (CollUtil.isEmpty(cvConversationDOList)) {
            return Collections.emptyList();
        }

        return cvConversationDOList.stream().map(CvConversationDO::getId).toList();
    }

    @Override
    public Long getTargetUserId(Long conversationId) {
        CvConversationDO cvConversationDO = cvConversationMapper.selectOne(
                new LambdaQueryWrapperX<CvConversationDO>()
                        .eq(CvConversationDO::getId, conversationId)
        );
        if (ObjUtil.isNotEmpty(cvConversationDO)) {
            return cvConversationDO.getCreateUserId().equals(SecurityFrameworkUtil.getLoginUserId())
                    ? cvConversationDO.getTargetUserId() : cvConversationDO.getCreateUserId();
        }
        return null;
    }

}
